// pages/app/certification/exam/exam.js
const app = getApp();
var host = app.globalData.host;
var utils = app.globalData.utils;
var self = null;

Page({
  data: {
    themeColor: '',
    themeColor2: '',
    defaultColor: '',
    borderColor: '',
    certificateItemId: '',
    list: [],
    currNo: 0,
    answers: []
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
    self.setData({
      certificateItemId: options.id
    })
    self.init();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor,
      themeColor2: app.globalData.themeColor2,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  init() {
    var url = host + '/lx/mini/question/exam';
    var params = {
      certificateItemId: self.data.certificateItemId
    };
    var success = function (res) {
      var list = res.info;
      self.setData({
        list: list
      })
      app.globalData.lastQuestions = list;
      // console.log(list);

      var templist = new Array;
      for (var i = 0; i < self.data.list.length; i++) {
        if (self.data.list[i].type == 1) {
          templist.push("");
        } else {
          templist.push(new Array);
        }
      }
      self.setData({
        answers: templist
      })
    }
    var fail = function (res) {
      console.log(res);
    };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  select(event) {
    var answerId = event.currentTarget.dataset.answerid;
    var answers = self.data.answers;
    answers[self.data.currNo] = Number(answerId);
    self.setData({
      answers: answers
    })
  },
  selectMulti(event) {
    var answerId = event.currentTarget.dataset.answerid;
    var answers = self.data.answers;
    if (!answers[self.data.currNo]) {
      answers[self.data.currNo] = new Array(self.data.list[self.data.currNo].answers.length);
      // answers[self.data.currNo] = new Array(4); // 答案个数
    }
    if (!answers[self.data.currNo][Number(answerId)]) {
      answers[self.data.currNo][Number(answerId)] = 1;
    } else {
      answers[self.data.currNo][Number(answerId)] = 0;
    }
    self.setData({
      answers: answers
    })
  },
  submit(event) {
    app.globalData.lastAnswers = self.data.answers;

    var url = host + '/lx/mini/question/submitExam';
    var params = {
      certificateItemId: self.data.certificateItemId,
      answersJson: JSON.stringify(self.data.answers)
    };
    var success = function (res) {
      if (res.info.isAllPass != 1) {
        if (res.info.isPass == 0) {
          app.globalData.lastWrongList = res.info.wrongList;
        }
        wx.redirectTo({
          url: '/pages/app/certification/result/result?id=' + self.data.certificateItemId + '&isPass=' + res.info.isPass + '&correctCount=' + res.info.correctCount + '&total=' + res.info.total + '&coursesLeft=' + res.info.coursesLeft
        })
      } else {
        wx.redirectTo({
          url: '/pages/app/certification/resultAllPass/resultAllPass?id=' + res.info.certificateId
        })
      }
    }
    var fail = function (res) {
      console.log(res);
    };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  prevQuest(event) {
    if (self.data.currNo > 0) {
      self.setData({
        currNo: self.data.currNo - 1
      })
    }
  },
  nextQuest(event) {
    if (self.data.currNo < self.data.list.length) {
      self.setData({
        currNo: self.data.currNo + 1
      })
    }
  }
})